﻿using System;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Sunny.UI;
using NewAndong.Comm;
using NewAndong.Model;
using Leeder.Logger;
using System.Collections.Generic;
using LeederGlobal;
using NewAndong.Handle;
using LEDDisplay.SDK;
using System.Threading;

namespace NewAndong.Forms
{
    public partial class FrmDeviceMonitor :  UIPage
    {

        AutoSizeFormClass asc = new AutoSizeFormClass();
        List<UIButton> btns = new List<UIButton>();
        public LogOperate LogObj => Log.GetLogger(this.GetType());
        bool isLoad = true;
        NewAndongHandler NewAndongHandler;


        public FrmDeviceMonitor()
        {
            InitializeComponent();
        }

        private void FrmDeviceMonitor_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;
            isLoad = false;
            //asc.controllInitializeSize(this);
            UpdateUI();
            NewAndongHandler = NewAndongHandler.CreateInstance();
            NewAndongHandler.UpsChangeEvent += UpdateUps;
            NewAndongHandler.OnDeviceStateChanged += DeviceStateChanged;
            var systemDevices = GolbalVariable.SystemDeviceBag.Where(x => (x.DataSource == "LED") && x.Enabled).ToList();
            systemDevices.ForEach(x =>
            {
                UIButton btn = new UIButton() { Margin = new Padding(30,10,30,10), Size = new Size(185,65), FillColor = Color.FromArgb(80, 160, 255), Name = x.DeviceCode, Text = x.DeviceName};
                btn.Click += btn_Click;
                btns.Add(btn);
                flowLayoutPanel.Controls.Add(btn);
            });
        }

        public void DeviceStateChanged(SystemDeviceModel model)
        {
            switch (model.DataSource)
            {
                case "LED":
                    UpdateLed(model);
                    break;
                case "IP":
                    UpdateUI_Nat(model);
                    break;
            }
        }

        private void btn_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            var btn = (UIButton)sender;

            var led = GolbalVariable.SystemDeviceBag.Where(x => x.DataSource == "LED" && x.DeviceName == btn.Text).FirstOrDefault();
            if(led.Status == 0)
            {
                UIMessageBox.Show("该安东屏通讯异常，无法执行开关屏操作，请检查网络！");
                return;
            }
            //操作单个安东屏开或关，逻辑未实现
            FrmLedOperatoin frmLedOperatoin = new FrmLedOperatoin(btn.Text);
            frmLedOperatoin.ShowDialog();
        }

        private void UpdateUI()
        {
            this.Invoke(new MethodInvoker(() =>
            {
                UpdateUps(GolbalVariable.UpsStatus);
                var pingList = GolbalVariable.SystemDeviceBag.Where(x => x.DataSource == "LED").ToList();
                foreach ( var ping in pingList )
                {
                    DeviceStateChanged(ping);
                }
            }));
        }

        /// <summary>
        /// 更新LED屏状态
        /// </summary>
        private void UpdateLed(SystemDeviceModel model)
        {
            var btn = btns.Where(p => p.Text == model.DeviceName).FirstOrDefault();
            if (btn != null)
            {
                if (model.Status == 1)
                {
                    btn.FillColor = Color.FromArgb(80, 160, 255);
                    btn.RectColor = Color.FromArgb(80, 160, 255);
                    btn.FillHoverColor = Color.FromArgb(111, 168, 255);
                }
                else
                {
                    btn.FillColor = Color.Red;
                    btn.RectColor = Color.Red;
                    btn.FillHoverColor = Color.FromArgb(255, 128, 128);
                }
            }
        }

        /// <summary>
        /// 更新LED屏状态
        /// </summary>
        private void UpdateLed(List<SystemDeviceModel> list)
        {
            foreach (var item in list)
            {
                var btn = btns.Where(p => p.Text == item.DeviceName).FirstOrDefault();
                if(btn != null)
                {
                    if (item.Status == 1)
                    {
                        btn.FillColor = Color.FromArgb(80, 160, 255);
                        btn.RectColor = Color.FromArgb(80, 160, 255);
                        btn.FillHoverColor = Color.FromArgb(111, 168, 255);
                    }
                    else
                    {
                        btn.FillColor = Color.Red;
                        btn.RectColor = Color.Red;
                        btn.FillHoverColor = Color.FromArgb(255, 128, 128);     
                    }
                }
            }
        }

        private void UpdateUI_Nat(SystemDeviceModel model)
        {
            var controls = this.Controls.Find(model.DeviceCode,false);
            if (controls == null|| controls.Length==0)
                return;
            var btn = (UIButton)controls[0];
            if(model.Status == 1)
            {
                btn.FillColor = Color.FromArgb(80, 160, 255);
                btn.RectColor = Color.FromArgb(80, 160, 255);
                btn.FillHoverColor = Color.FromArgb(111, 168, 255);
            }
            else
            {
                btn.FillColor = Color.Red;
                btn.RectColor = Color.Red;
                btn.FillHoverColor = Color.FromArgb(255, 128, 128);
            }
        }

        /// <summary>
        /// 刷新UPS状态数据
        /// </summary>
        /// <param name="model"></param>
        private void UpdateUps(UpsModel model)
        {
            if (model == null)
                return;
            if (model.State != null && model.QueryResult)
            {
                if (model.State.UpsFault == 0)
                {
                    if (model.State.BatteryVoltage == 1)
                    {
                        //电池电压低
                        btnUps.Text = "UPS电池电压低";
                        btnUps.FillColor = Color.Red;
                        btnUps.RectColor = Color.Red;
                    }
                    else
                    {
                        //电池正常
                        btnUps.Text = "UPS电源";
                        btnUps.FillColor = Color.FromArgb(80, 160, 255);
                        btnUps.RectColor = Color.FromArgb(80, 160, 255);
                    }
                }
                else
                {
                    //UPS故障
                    btnUps.Text = "UPS故障";
                    btnUps.FillColor = Color.Red;
                    btnUps.RectColor = Color.Red;
                }
            }
            else
            {
                //其他异常
                //btnUps.Text = "UPS通讯异常";
                //btnUps.FillColor = Color.Red;
                //btnUps.RectColor = Color.Red;
            }
        }

        private void FrmDeviceMonitor_SizeChanged(object sender, EventArgs e)
        {
            //asc.controlAutoSize(this);
        }

        private void FrmDeviceMonitor_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void btnOpenAllLed_Click(object sender, EventArgs e)
        {
            //if (ApplicationCache.CurrentUser.Permission > 1)
            //{
            //    UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
            //    return;
            //}
            ShowWaitForm("正在打开安东屏，请稍候...");
            NewAndongHandler.OpenAllLed();
            HideWaitForm();
        }

        private void btnCloseAllLed_Click(object sender, EventArgs e)
        {
            //if (ApplicationCache.CurrentUser.Permission > 1)
            //{
            //    UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
            //    return;
            //}
            if (!UIMessageDialog.ShowMessageDialog("确定要关闭全部安东屏吗？", UILocalize.AskTitle, true, UIStyle.Red, true, false))
            {
                return;
            }
            ShowWaitForm("正在关闭安东屏，请稍候...");
            NewAndongHandler.CloseAllLed();
            HideWaitForm();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            NewAndongHandler.PicsGroup(uiTextBox1.Text, int.Parse(uiTextBox2.Text));
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            NewAndongHandler.LipGroup(uiTextBox4.Text, 1);
            NewAndongHandler.LipGroup(uiTextBox3.Text, 1);
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            var r1 = 1 % 3 == 1 ? false : true;
            var r2 = 2 % 3 == 1 ? false : true;
            var r3 = 3 % 3 == 1 ? false : true;
            var r4 = 4 % 3 == 1 ? false : true;
            var r5 = 5 % 3 == 1 ? false : true;
            var r6 = 6 % 3 == 1 ? false : true;
            var r7 = 7 % 3 == 1 ? false : true;
            var r8 = 8 % 3 == 1 ? false : true;
        }
    }
}
